package com.example.animationlearn;

import android.app.Activity;
import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

public class NotificationActivity extends Activity {
    private static final String TAG = "NotificationActivity";

    private LinearLayout linearLayout;

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        linearLayout = findViewById(R.id.linearLayout);
        button = findViewById(R.id.button);
        IntentFilter intentFilter = new IntentFilter("com.ssy.myintnent.action");
        registerReceiver(mRemoteViewReceiver, intentFilter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(NotificationActivity.this, MainActivity.class);
                Intent intent = new Intent();
                ComponentName componentName =
                        new ComponentName("com.example.anotheractivity", "com.example.anotheractivity.MainActivity");
                intent.setComponent(componentName);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }


    private BroadcastReceiver mRemoteViewReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //  Toast.makeText(context, "+++++++", Toast.LENGTH_SHORT).show();
            Log.e("mytag", "--");
            RemoteViews remoteViews = intent.getParcelableExtra("com.ssy.myintnent.remoteview");
            Log.i(TAG, "onReceive: remoteview" + remoteViews);
            Log.i(TAG, "onReceive: remoteViews.getLayoutId()" + remoteViews.getLayoutId());
            if (remoteViews != null) {
                Log.e(TAG, "onReceive: remoteViews != null");
                int layout_id = getResources().getIdentifier("layout_simulated_notification", "layout", getPackageName());
                Log.i(TAG, "onReceive: layout_id" + layout_id);
//                View view =  getLayoutInflater().inflate(layout_id, linearLayout, false);
                View view =  getLayoutInflater().inflate(layout_id, linearLayout, false);
                Log.i(TAG, "onReceive: view"+view);
//                remoteViews.reapply(context, view);
                remoteViews.reapply(context, view);
                //  View view = remoteViews.apply(NotificationActivity.this,li_1);
                //  View view = remoteViews.apply(NotificationActivity.this,li_1);
                linearLayout.addView(view);
            } else {
                Log.i(TAG, "onReceive: remoteViews is null");
            }
        }
    };
}
